import { centerMap, GETNOBASE } from "@/api";
import { registerMap, getMap } from "echarts/core";
/**
 * 从福建省 GeoJSON 中提取 9 个地级市列表
 * @returns Array<{ name: string, value: [number, number, number], adcode: string }>
 */
export const getGeojson = (regionCode: string) => {
    return new Promise<boolean>(async resolve => {
        let mapjson = getMap(regionCode);
        if (mapjson) {
            mapjson = mapjson.geoJSON;
            resolve(mapjson);
        } else {
            mapjson = await GETNOBASE(`./map-geojson/${regionCode}.json`).then(data => data);
            registerMap(regionCode, {
                geoJSON: mapjson as any,
                specialAreas: {},
            });
            resolve(mapjson);
        }
    });
};

export const getFujianCities = async (regionCode: string) => {
    const geojson: any = await getGeojson(regionCode);
    // 2. 建立城市名 -> 中心点索引
    let list: { name: any; value: any; center: any; centroid: any; }[] = [];
    geojson.features.forEach((feature: any) => {
        const { name, centroid, center } = feature.properties as any;
        list.push({ name, value: center, center, centroid });
    });
    return list;
};
